package Game.Windows 
{
	import Box2D.Common.Math.b2Math;
	import Box2D.Common.Math.b2Vec2;
	import Common.Utils;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.getTimer;
	import flash.utils.Timer;
	import Game.GameElements.BattleTower;
	import Game.GameElements.GameObject;
	import Game.GameElements.Tank;
	/**
	 * ...
	 * @author Yevgeniy Logachev
	 */
	public class MiniMap 
	{
		[Embed(source="../../../res/swf/miniMap.swf", symbol="radar")]
		public static var RadarClass:Class;
		
		[Embed(source="../../../res/swf/miniMap.swf", symbol="sector")]
		public static var SectorClass:Class;
		
		public static 	var 	m_miniMap:Sprite 		= null;
		public static 	var 	m_sector:Sprite 		= null;
		private static 	var 	m_show:Boolean			= false;
		
		private static 	const 	k_ANGLE_SPEED:int 		= 5;
		private static 	const  	k_SCAN_RADIUS:int 		= 800;
		
		public static	var 	m_lastTimeUpdate:uint 	= getTimer();
		public static	var 	m_refrashTime:uint 		= 30;
				
		
		public static function set show(value:Boolean):void
		{
			m_show = value;
			if (m_show && m_miniMap == null)
			{
				m_miniMap = new RadarClass();
				m_sector = new SectorClass();
				
				m_miniMap.x = Settings.screenWidth - m_miniMap.width / 2;
				m_miniMap.y = Settings.screenHeight - m_miniMap.height / 2;
				m_miniMap.addChild(m_sector);
				
				Main.mainSprite.stage.addChild(m_miniMap);	
			}
			else if(m_miniMap != null)
			{
				Main.mainSprite.stage.removeChild(m_miniMap);
				m_miniMap = null;
			}
		}
		
		public static function get show():Boolean
		{
			return m_show;
		}
		
		public static function update():void
		{
			if (m_show && (getTimer() - m_lastTimeUpdate > m_refrashTime))
			{
				m_lastTimeUpdate = getTimer();
								
				for each(var object:GameObject in GameObject.gameObjects)
				{
					if (object.isEnemy && Tank.getGamePlayer() != null)
					{
						var flag:Boolean = true;
						for (var i:int = 0; i < m_miniMap.numChildren; ++i)
						{
							if ((m_miniMap.getChildAt(i) is TargetPoint) &&
								((m_miniMap.getChildAt(i) as TargetPoint).m_targetReference == object))
							{
								flag = false;
							}
						}
						
						if (flag)
						{
							var vec1:b2Vec2 = new b2Vec2(object.x - Tank.getGamePlayer().x, object.y - Tank.getGamePlayer().y);
							var vec2:b2Vec2 = Utils.getDirection(m_sector);
							
							if ((vec1.Length() < k_SCAN_RADIUS) && 
								(Math.abs.apply(null, [(Math.atan2(vec2.y, vec2.x) - Math.atan2(vec1.y, vec1.x)) * 180.0 / Math.PI]) < 2 * k_ANGLE_SPEED))
							{
								var target:TargetPoint = new TargetPoint(object, removeTargetPoint);
								target.x = vec1.x / k_SCAN_RADIUS * m_miniMap.width / 2.5;
								target.y = vec1.y / k_SCAN_RADIUS * m_miniMap.height / 2.5;
								
								m_miniMap.addChild(target);
							}
						}
					}
				}
				
				m_sector.rotation += k_ANGLE_SPEED;
			}
		}
		
		public static function removeTargetPoint(targetPoint:TargetPoint):void
		{
			for (var i:int = 0; i < m_miniMap.numChildren; ++i)
			{
				if ((m_miniMap.getChildAt(i) is TargetPoint) &&
					(targetPoint == m_miniMap.getChildAt(i) as TargetPoint))
				{
					m_miniMap.removeChild(targetPoint);
					targetPoint.release();
					targetPoint = null;
				}
			}
		}
	}
}


import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import Game.GameElements.GameObject;
import Game.GameElements.Tank;

class TargetPoint extends Sprite
{
	[Embed(source="../../../res/swf/miniMap.swf", symbol="target")]
	public static var TargetClass:Class;
	
	public var m_targetReference:GameObject 	= null;
	public var m_timerShow:Timer 		= null;
	public var m_removeHandler:Function	= null;
	
	public function TargetPoint(targetReference:GameObject, removeHandler:Function)
	{
		m_targetReference = targetReference;
		m_removeHandler = removeHandler;
		
		addChild(new TargetClass());
		
		m_timerShow = new Timer(500);
		m_timerShow.addEventListener(TimerEvent.TIMER, onRemove);
		m_timerShow.start();
	}
	
	public function onRemove(e:TimerEvent):void
	{
		m_timerShow.removeEventListener(TimerEvent.TIMER, onRemove);
		m_timerShow = null;
		m_targetReference = null;
		
		m_removeHandler(this);
	}
	
	public function release():void
	{
		m_targetReference = null;
		m_removeHandler = null;
		if (m_timerShow != null)
		{
			m_timerShow.removeEventListener(TimerEvent.TIMER, onRemove);
			m_timerShow = null;
		}
	}
}